package club.emergency.wechat.question_collection.search.provider.impl;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.model.question_collection.QuestionCollection;
import club.emergency.wechat.persistence.question.QuestionMapper;
import club.emergency.wechat.persistence.question_collection.QuestionCollectionMapper;
import club.emergency.wechat.question_collection.search.QuestionCollectionSearch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.question_collection.search.provider.impl
 * Date: 2019/2/21
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
public class QuestionCollectionSearchImpl implements QuestionCollectionSearch {

    private QuestionCollectionMapper questionCollectionMapper;

    private QuestionMapper questionMapper;

    public QuestionCollectionSearchImpl(QuestionCollectionMapper questionCollectionMapper, QuestionMapper questionMapper) {
        this.questionCollectionMapper = questionCollectionMapper;
        this.questionMapper = questionMapper;
    }

    @Override
    public String flipListByStaffId(
            @RequestParam("staffId") Long staffId,
            @RequestParam(value = "categoryAlias", required = false) String categoryAlias,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize
    ) {
        Page page = new Page<>(pageNo, pageSize);
        QueryWrapper<QuestionCollection> questionCollectionQueryWrapper = new QueryWrapper<>();
        questionCollectionQueryWrapper.eq("staff_id", staffId);
        questionCollectionQueryWrapper.eq(Objects.nonNull(categoryAlias), "category_alias", categoryAlias);
        IPage questionCollectionIPage = questionCollectionMapper.selectPage(page, questionCollectionQueryWrapper);
        List<QuestionCollection> records = questionCollectionIPage.getRecords();
        if (records.size() == 0) {
            return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "根据人员id查询该人员所有收藏的试题成功！", questionCollectionIPage);
        }
        List<Long> ids = new ArrayList<>();
        records.forEach(questionCollection -> ids.add(questionCollection.getQuestionId()));
        questionCollectionIPage.setRecords(questionMapper.selectBatchIds(ids));
        return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "根据人员id查询该人员所有收藏的试题成功！", questionCollectionIPage);
    }

    @Override
    public String checkIsCollection(@RequestParam("staffId") Long staffId, @RequestParam("questionId") Long questionId) {
        QueryWrapper<QuestionCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_id", staffId);
        queryWrapper.eq("question_id", questionId);
        Integer count = questionCollectionMapper.selectCount(queryWrapper);
        if (count.equals(0)) {
            return ResponseEntity.ok(StatusCode.DB_SEARCH_ERROR, "该试题没有被收藏！");
        } else {
            return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "该试题已经被收藏！");
        }
    }
}
